<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>垂直居中</title>
  <style>
    .box {
      height: 100px;
      border: 1px solid black;
    }

    .box-1 {
      text-align: center;
      line-height: 100px;
    }

    .box-2 {
      position: relative;
    }
    .box-2 .item {
      position: absolute;
      left: 50%;
      top: 50%;
      margin-left: -100px;
      margin-top: -25px;
      width: 200px;
      height: 50px;
      background-color: lightblue;
    }

    .box-3 {
      position: relative;
    }

    .box-3 .item {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 200px;
      height: 75px;
      background-color: lightcoral;
    }

    .box-4 {
      position: relative;
    }

    .box-4 .item {
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      margin: auto;
      width: 200px;
      height: 50px;
      background-color: lightcoral;
    }




  </style>
</head>

<body>
  <div class="box box-1">
    <span>content</span>
  </div>

  <!-- absolute定位元素 -->
  <div class="box box-2">
    <div class="item">Item</div>
  </div>

  <!-- absolute定位元素 -->
  <div class="box box-3">
    <div class="item">Item</div>
  </div>

  <!-- absolute定位元素 -->
  <div class="box box-4">
    <div class="item">Item</div>
  </div>
</body>

</html>